package com.qk.service.impl;

import com.qk.mapper.BusinessMapper;
import com.qk.mapper.ClueMapper;
import com.qk.service.ReportService;
import com.qk.vo.OverviewVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ClueMapper clueMapper;
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    @Override
    public OverviewVo getOverview() {
        //1.从Redis缓存中获取数据概览
        log.info("从Redis缓存中获取数据概览");
        Object object = redisTemplate.opsForValue().get("overview");
        //2.判断Redis缓存中有没有数据,有就直接返回,如果没有就去数据库中查询
        if(object != null){
            log.info("从Redis缓存中获取数据概览成功");
            return (OverviewVo) object;
        }
        //3.1数据库中获取线索数据概览
         OverviewVo ClueOverview =clueMapper.getOverviewData();
        //3.2获取商机数据概览
         OverviewVo BusinessOverview =businessMapper.getOverviewData();
        //3.3合并资源
        BeanUtils.copyProperties(ClueOverview,BusinessOverview,"businessTotal","businessWaitAllot","businessWaitFollow","businessFollowing","businessFalse","businessConvertCustomer");

        //4.缓存Redis
        log.info("缓存到Redis中");
        redisTemplate.opsForValue().set("overview",BusinessOverview,1, TimeUnit.MINUTES);

        return BusinessOverview;
    }
}
